package com.youku.plugins.endcardV4New.view
{
   import flash.display.Sprite;
   import com.youku.interfaces.IEndCardV4New;
   import com.youku.plugins.PluginEventDispather;
   import com.youku.baseUI.BasePlayerButton;
   import flash.utils.Timer;
   import com.youku.plugins.endcardV4New.model.data.LoadImageList;
   import flash.text.TextField;
   import flash.display.MovieClip;
   import com.youku.plugins.endcardV4New.model.data.PlayerSizeData;
   import flash.text.StyleSheet;
   import com.youku.plugins.endcardV4New.model.data.RelativeVideoData;
   import com.youku.utils.AssetUtil;
   import flash.text.TextFormat;
   import com.youku.skin.YaheiFontManager;
   import flash.text.TextFieldAutoSize;
   import flash.events.TextEvent;
   import com.youku.interfaces.IRelativeVideoList;
   import com.youku.plugins.endcardV4New.model.data.PlayerVideoData;
   import com.youku.plugins.endcardV4New.EndCardConstant;
   import flash.display.DisplayObject;
   import flash.events.MouseEvent;
   import flash.events.TimerEvent;
   import com.youku.plugins.endcardV4New.EndCardEvent;
   import flash.display.Bitmap;
   import flash.display.GradientType;
   import com.youku.utils.DrawUtil;
   import com.youku.utils.Tweener;
   
   public class Relative extends Sprite implements IEndCardV4New
   {
      
      public function Relative() {
         super();
      }
      
      private var _videoDataArray:Array;
      
      private var _eventManager:PluginEventDispather;
      
      private var _itemMcArray:Vector.<RelativePreviewBox>;
      
      private var _relativePanelArray:Vector.<RelativePanel>;
      
      private var _nextBtn:BasePlayerButton;
      
      private var _prevBtn:BasePlayerButton;
      
      private var _interaction:Interaction;
      
      private var _currentRelativePanelNum:int;
      
      private var _preRelativePanelNum:int;
      
      private var _previewBoxWidth:Number = 200;
      
      private var _previewBoxHeight:Number = 110;
      
      private var _rowNum:int;
      
      private var _columnNum:int;
      
      private var _boxSpace:Number = 10;
      
      private var _width:Number;
      
      private var _height:Number;
      
      private var _oneScreenNum:uint;
      
      private var _screenNum:uint;
      
      private var _runTimeMcArray:Array;
      
      private var _runTimeMcLocArray:Array;
      
      private var _timer:Timer;
      
      private var _changeScreenTime:uint = 8000.0;
      
      private var _nowScreenNum:int = 0;
      
      private var _loadImgList:LoadImageList;
      
      private var _isInit:Boolean = false;
      
      private var _dataErrorTextField:TextField;
      
      private var _dataErrorIcon:MovieClip;
      
      private var _videoClickTimer:Timer;
      
      public function init(param1:Object = null, param2:PluginEventDispather = null, param3:PlayerSizeData = null) : void {
         var _loc8_:StyleSheet = null;
         var _loc9_:RelativePanel = null;
         var _loc10_:RelativeVideoData = null;
         var _loc11_:RelativePreviewBox = null;
         this._eventManager = param2;
         if(!param1 || !param1.relativeVideoList || !param1.playerVideoData)
         {
            this._dataErrorIcon = AssetUtil.getAsset("E_Error_Icon") as MovieClip;
            this._dataErrorIcon.x = 0;
            this._dataErrorIcon.y = 0;
            this._dataErrorTextField = AssetUtil.getTextField(false);
            this._dataErrorTextField.defaultTextFormat = new TextFormat(YaheiFontManager.MICROSOFT_YAHEI,14,16777215);
            this._dataErrorTextField.autoSize = TextFieldAutoSize.LEFT;
            this._dataErrorTextField.x = 34;
            this._dataErrorTextField.y = 2;
            _loc8_ = new StyleSheet();
            _loc8_.parseCSS("a{color:#06a7e1;}a:hover{color:#cc3311;}");
            this._dataErrorTextField.styleSheet = _loc8_;
            this._dataErrorTextField.htmlText = "数据加载失败,请<a href=\'event:geturl|{url}\'>刷新<a>";
            this._dataErrorTextField.addEventListener(TextEvent.LINK,this.onTextLink);
            addChild(this._dataErrorIcon);
            addChild(this._dataErrorTextField);
            return;
         }
         this._isInit = true;
         this._rowNum = 0;
         this._columnNum = 0;
         var _loc4_:IRelativeVideoList = param1.relativeVideoList as IRelativeVideoList;
         var _loc5_:PlayerVideoData = param1.playerVideoData as PlayerVideoData;
         this._videoDataArray = _loc4_.videoList;
         this._itemMcArray = new Vector.<RelativePreviewBox>();
         this._relativePanelArray = new Vector.<RelativePanel>();
         var _loc6_:* = 0;
         while(_loc6_ < 2)
         {
            _loc9_ = new RelativePanel(_loc6_);
            _loc9_.x = 0;
            _loc9_.y = 0;
            _loc9_.visible = false;
            this._relativePanelArray.push(_loc9_);
            addChild(_loc9_);
            _loc6_++;
         }
         this._currentRelativePanelNum = 0;
         this._nextBtn = AssetUtil.getStyleButton("EndCard_navigate_button_right");
         AssetUtil.restoreButtonSize(this._nextBtn);
         this._nextBtn.visible = false;
         this._prevBtn = AssetUtil.getStyleButton("EndCard_navigate_button_left");
         AssetUtil.restoreButtonSize(this._prevBtn);
         this._prevBtn.visible = false;
         this._interaction = new Interaction();
         this._interaction.init(_loc5_,param2,param3);
         this._interaction.setWH(this._previewBoxWidth,this._previewBoxHeight);
         this.addChild(this._nextBtn);
         this.addChild(this._prevBtn);
         this._loadImgList = LoadImageList.getInstance();
         var _loc7_:* = 0;
         while(_loc7_ < this._videoDataArray.length)
         {
            _loc10_ = this._videoDataArray[_loc7_] as RelativeVideoData;
            _loc11_ = new RelativePreviewBox(_loc7_,_loc10_);
            _loc11_.setWH(this._previewBoxWidth,this._previewBoxHeight);
            this._itemMcArray.push(_loc11_);
            this._loadImgList.addtoList(_loc10_.logo,EndCardConstant.RELATIVE_PREVIEW_IMAGE,String(_loc7_));
            _loc7_++;
         }
         this._timer = new Timer(this._changeScreenTime);
         this._videoClickTimer = new Timer(100);
         this._videoClickTimer.reset();
         this._videoClickTimer.start();
         this.changeSize(param3);
         this.addEvent();
         this._loadImgList.load();
      }
      
      public function changeSize(param1:PlayerSizeData = null) : void {
         var _loc5_:Array = null;
         var _loc6_:Array = null;
         var _loc7_:* = 0;
         var _loc8_:DisplayObject = null;
         if(!this._videoDataArray)
         {
            return;
         }
         this.visible = true;
         this._width = param1.width;
         this._height = param1.height;
         this._rowNum = param1.rowNum;
         this._columnNum = param1.columnNum;
         this._oneScreenNum = this._columnNum * this._rowNum;
         this._screenNum = Math.floor((this._itemMcArray.length + 1) / this._oneScreenNum);
         if(this._screenNum < 1)
         {
            this._screenNum = 1;
         }
         this._interaction.changeSize(param1);
         this._runTimeMcArray = [];
         this._runTimeMcLocArray = [];
         if(this._screenNum <= 1)
         {
            this._nextBtn.visible = false;
            this._prevBtn.visible = false;
         }
         else
         {
            this._nextBtn.visible = true;
            this._prevBtn.visible = true;
            this._prevBtn.x = ((this._previewBoxWidth + this._boxSpace) * this._columnNum - this._boxSpace) / 2 - this._prevBtn.width - 15;
            this._prevBtn.y = (this._previewBoxHeight + this._boxSpace) * this._rowNum;
            this._nextBtn.x = this._prevBtn.x + this._prevBtn.width + 30;
            this._nextBtn.y = this._prevBtn.y;
         }
         var _loc2_:* = 0;
         var _loc3_:* = 0;
         while(_loc3_ < this._screenNum)
         {
            _loc5_ = [];
            _loc6_ = [];
            _loc7_ = 0;
            while(_loc7_ < this._oneScreenNum)
            {
               if(_loc2_ >= this._itemMcArray.length)
               {
                  break;
               }
               if(this._itemMcArray[_loc2_])
               {
                  if(_loc3_ == 0 && _loc7_ == this._columnNum - 1)
                  {
                     _loc5_.push(this._interaction);
                  }
                  else
                  {
                     _loc5_.push(this._itemMcArray[_loc2_]);
                     _loc2_++;
                  }
                  _loc6_.push(
                     {
                        "x":(this._previewBoxWidth + this._boxSpace) * Math.floor(_loc7_ % this._columnNum),
                        "y":(this._previewBoxHeight + this._boxSpace) * Math.floor(_loc7_ / this._columnNum)
                     });
                  _loc7_++;
                  continue;
               }
               break;
            }
            this._runTimeMcArray.push(_loc5_);
            this._runTimeMcLocArray.push(_loc6_);
            _loc3_++;
         }
         this._relativePanelArray[this._currentRelativePanelNum].removeAllBox();
         this._relativePanelArray[this._preRelativePanelNum].removeAllBox();
         this._nowScreenNum = this._nowScreenNum % this._screenNum;
         var _loc4_:* = 0;
         while(_loc4_ < this._runTimeMcArray[this._nowScreenNum].length)
         {
            _loc8_ = this._runTimeMcArray[this._nowScreenNum][_loc4_] as DisplayObject;
            this._relativePanelArray[this._currentRelativePanelNum].addBox(_loc8_,this._runTimeMcLocArray[this._nowScreenNum][_loc4_].x,this._runTimeMcLocArray[this._nowScreenNum][_loc4_].y);
            _loc4_++;
         }
         this._relativePanelArray[this._currentRelativePanelNum].visible = true;
         if(this._screenNum <= 1)
         {
            this._timer.stop();
         }
         else
         {
            this._timer.reset();
            this._timer.start();
         }
      }
      
      private function addEvent() : void {
         var _loc2_:RelativePreviewBox = null;
         this._nextBtn.addEventListener(MouseEvent.CLICK,this.onNextBtnClick);
         this._prevBtn.addEventListener(MouseEvent.CLICK,this.onPrevBtnClick);
         this._nextBtn.addEventListener(MouseEvent.ROLL_OVER,this.onBtnOver);
         this._prevBtn.addEventListener(MouseEvent.ROLL_OVER,this.onBtnOver);
         this._interaction.addEventListener(MouseEvent.ROLL_OVER,this.onBtnOver);
         this._nextBtn.addEventListener(MouseEvent.ROLL_OUT,this.onBtnOut);
         this._prevBtn.addEventListener(MouseEvent.ROLL_OUT,this.onBtnOut);
         this._interaction.addEventListener(MouseEvent.ROLL_OUT,this.onBtnOut);
         var _loc1_:* = 0;
         while(_loc1_ < this._itemMcArray.length)
         {
            _loc2_ = this._itemMcArray[_loc1_];
            _loc2_.addEventListener(MouseEvent.ROLL_OVER,this.onBtnOver);
            _loc2_.addEventListener(MouseEvent.ROLL_OUT,this.onBtnOut);
            _loc2_.addEventListener(MouseEvent.CLICK,this.onPreviewBoxClick);
            _loc1_++;
         }
         this._timer.addEventListener(TimerEvent.TIMER,this.onChangeScreenTimer);
         this._loadImgList.addEventListener(EndCardEvent.RELATIVE_IMAGE_LOAD_COMPLETE,this.onImgLoadComplete);
      }
      
      private function onTextLink(param1:TextEvent) : void {
         this._eventManager.dispatchEvent(new EndCardEvent(EndCardEvent.ENDCARD_RESET));
      }
      
      private function onImgLoadComplete(param1:EndCardEvent) : void {
         var _loc2_:Bitmap = null;
         var _loc3_:RelativePreviewBox = null;
         switch(param1.data.type)
         {
            case EndCardConstant.RELATIVE_PREVIEW_IMAGE:
               _loc2_ = param1.data.img as Bitmap;
               _loc3_ = this._itemMcArray[Number(param1.data.subType)] as RelativePreviewBox;
               try
               {
                  _loc2_.smoothing = true;
               }
               catch(e:Error)
               {
               }
               _loc3_.addImage(this.getImage(_loc2_));
               break;
         }
      }
      
      private function getImage(param1:Bitmap) : Sprite {
         var _loc2_:Number = this._previewBoxWidth;
         var _loc3_:Number = this._previewBoxHeight;
         if(_loc2_ / _loc3_ >= param1.width / param1.height)
         {
            param1.scaleX = param1.scaleY = _loc2_ / param1.width;
            param1.x = 0;
            param1.y = -(param1.height - _loc3_) / 2;
         }
         else
         {
            param1.scaleX = param1.scaleY = _loc3_ / param1.height;
            param1.y = 0;
            param1.x = -(param1.width - _loc2_) / 2;
         }
         var _loc4_:Sprite = new Sprite();
         _loc4_.mouseEnabled = false;
         _loc4_.mouseChildren = false;
         _loc4_.addChild(param1);
         var _loc5_:Array = [0,0];
         var _loc6_:String = GradientType.LINEAR;
         var _loc7_:Array = [0,0.7];
         var _loc8_:Array = [0,255];
         var _loc9_:Object = 
            {
               "gradientType":_loc6_,
               "colors":_loc5_,
               "alphas":_loc7_,
               "ratios":_loc8_,
               "angle":90
            };
         var _loc10_:Sprite = DrawUtil.RectangleSprite(0,0,100,100,0,1,_loc9_,0);
         _loc10_.width = _loc2_;
         _loc10_.height = _loc3_;
         _loc4_.addChild(_loc10_);
         var _loc11_:Sprite = new Sprite();
         _loc11_.graphics.beginFill(39372);
         _loc11_.graphics.drawRect(0,0,_loc2_,_loc3_);
         _loc11_.graphics.endFill();
         _loc4_.addChild(_loc11_);
         _loc4_.mask = _loc11_;
         return _loc4_;
      }
      
      private function showPreScreen() : void {
         this._nowScreenNum--;
         if(this._nowScreenNum < 0)
         {
            this._nowScreenNum = this._screenNum - 1;
         }
         if(this._nowScreenNum < 0)
         {
            this._nowScreenNum = 0;
         }
         this.changeScreen(this._nowScreenNum,"pre");
      }
      
      private function showNextScreen() : void {
         this._nowScreenNum++;
         this._nowScreenNum = this._nowScreenNum % this._screenNum;
         this.changeScreen(this._nowScreenNum,"next");
      }
      
      private function changeScreen(param1:uint, param2:String) : void {
         var _loc4_:DisplayObject = null;
         this._preRelativePanelNum = this._currentRelativePanelNum;
         if(this._currentRelativePanelNum == 0)
         {
            this._currentRelativePanelNum = 1;
         }
         else if(this._currentRelativePanelNum == 1)
         {
            this._currentRelativePanelNum = 0;
         }
         
         this._relativePanelArray[this._currentRelativePanelNum].removeAllBox();
         var _loc3_:* = 0;
         while(_loc3_ < this._runTimeMcArray[this._nowScreenNum].length)
         {
            _loc4_ = this._runTimeMcArray[this._nowScreenNum][_loc3_] as DisplayObject;
            _loc4_.visible = true;
            this._relativePanelArray[this._currentRelativePanelNum].addBox(_loc4_,this._runTimeMcLocArray[this._nowScreenNum][_loc3_].x,this._runTimeMcLocArray[this._nowScreenNum][_loc3_].y);
            _loc3_++;
         }
         switch(param2)
         {
            case "next":
               this._relativePanelArray[this._preRelativePanelNum].x = 0;
               this._relativePanelArray[this._preRelativePanelNum].y = 0;
               this._relativePanelArray[this._currentRelativePanelNum].x = this._width / scaleX;
               this._relativePanelArray[this._currentRelativePanelNum].y = 0;
               this._relativePanelArray[this._currentRelativePanelNum].visible = true;
               Tweener.to(this._relativePanelArray[this._preRelativePanelNum],0.5,
                  {
                     "x":-this._width / scaleX,
                     "visible":0,
                     "onComplete":this.onChangeScreenComplete
                  });
               Tweener.to(this._relativePanelArray[this._currentRelativePanelNum],0.5,{"x":0});
               break;
            case "pre":
               this._relativePanelArray[this._preRelativePanelNum].x = 0;
               this._relativePanelArray[this._preRelativePanelNum].y = 0;
               this._relativePanelArray[this._currentRelativePanelNum].x = -this._width / scaleX;
               this._relativePanelArray[this._currentRelativePanelNum].y = 0;
               this._relativePanelArray[this._currentRelativePanelNum].visible = true;
               Tweener.to(this._relativePanelArray[this._preRelativePanelNum],0.5,
                  {
                     "x":this._width / scaleX,
                     "visible":0,
                     "onComplete":this.onChangeScreenComplete
                  });
               Tweener.to(this._relativePanelArray[this._currentRelativePanelNum],0.5,{"x":0});
               break;
         }
         if(this._screenNum <= 1)
         {
            this._timer.stop();
         }
         else
         {
            this._timer.reset();
            this._timer.start();
         }
         this._videoClickTimer.reset();
         this._videoClickTimer.start();
      }
      
      private function onChangeScreenComplete() : void {
         this._relativePanelArray[this._preRelativePanelNum].removeAllBox();
      }
      
      private function onChangeScreenTimer(param1:TimerEvent = null) : void {
         this.showNextScreen();
      }
      
      private function removeEvent() : void {
         var _loc2_:RelativePreviewBox = null;
         this._nextBtn.removeEventListener(MouseEvent.CLICK,this.onNextBtnClick);
         this._prevBtn.removeEventListener(MouseEvent.CLICK,this.onPrevBtnClick);
         this._nextBtn.removeEventListener(MouseEvent.ROLL_OVER,this.onBtnOver);
         this._prevBtn.removeEventListener(MouseEvent.ROLL_OVER,this.onBtnOver);
         this._nextBtn.removeEventListener(MouseEvent.ROLL_OUT,this.onBtnOut);
         this._prevBtn.removeEventListener(MouseEvent.ROLL_OUT,this.onBtnOut);
         var _loc1_:* = 0;
         while(_loc1_ < this._itemMcArray.length)
         {
            _loc2_ = this._itemMcArray[_loc1_] as RelativePreviewBox;
            _loc2_.removeEventListener(MouseEvent.ROLL_OVER,this.onBtnOver);
            _loc2_.removeEventListener(MouseEvent.ROLL_OUT,this.onBtnOut);
            _loc2_.removeEventListener(MouseEvent.CLICK,this.onPreviewBoxClick);
            _loc1_++;
         }
         this._timer.removeEventListener(TimerEvent.TIMER,this.onChangeScreenTimer);
         this._loadImgList.removeEventListener(EndCardEvent.RELATIVE_IMAGE_LOAD_COMPLETE,this.onImgLoadComplete);
      }
      
      private function onBtnOver(param1:MouseEvent) : void {
         this._timer.stop();
      }
      
      private function onBtnOut(param1:MouseEvent) : void {
         if(this._screenNum > 1)
         {
            this._timer.reset();
            this._timer.start();
         }
      }
      
      private function onPrevBtnClick(param1:MouseEvent) : void {
         var _loc2_:EndCardEvent = new EndCardEvent(EndCardEvent.ENDCARD_NAVIGATE_LEFT_CLICK);
         this._eventManager.dispatchEvent(_loc2_);
         this.showPreScreen();
      }
      
      private function onNextBtnClick(param1:MouseEvent) : void {
         var _loc2_:EndCardEvent = new EndCardEvent(EndCardEvent.ENDCARD_NAVIGATE_RIGHT_CLICK);
         this._eventManager.dispatchEvent(_loc2_);
         this.showNextScreen();
      }
      
      private function onPreviewBoxClick(param1:MouseEvent) : void {
         var _loc2_:RelativePreviewBox = param1.currentTarget as RelativePreviewBox;
         var _loc3_:EndCardEvent = new EndCardEvent(EndCardEvent.ENDCARD_RELATIVE_VIDEO_CLICK,
            {
               "link":_loc2_.link,
               "target":_loc2_.target,
               "ID":_loc2_.ID,
               "rData":this._videoDataArray[_loc2_.ID],
               "ts":this._videoClickTimer.currentCount / 10
            });
         this._eventManager.dispatchEvent(_loc3_);
      }
      
      override public function get width() : Number {
         if(!this._isInit)
         {
            return super.width;
         }
         return (this._previewBoxWidth + this._boxSpace) * this._columnNum - this._boxSpace;
      }
      
      override public function get height() : Number {
         if(!this._isInit)
         {
            return super.height;
         }
         if(this._screenNum <= 1)
         {
            return (this._previewBoxHeight + this._boxSpace) * this._rowNum - this._boxSpace;
         }
         return this._prevBtn.y + this._prevBtn.height;
      }
      
      override public function set scaleX(param1:Number) : void {
         if(!this._isInit)
         {
            return;
         }
         super.scaleX = param1;
      }
      
      override public function set scaleY(param1:Number) : void {
         if(!this._isInit)
         {
            return;
         }
         super.scaleY = param1;
      }
      
      public function uninstall() : void {
         var _loc2_:RelativePanel = null;
         if(!this._isInit)
         {
            if(contains(this._dataErrorTextField))
            {
               removeChild(this._dataErrorTextField);
            }
            if(contains(this._dataErrorIcon))
            {
               removeChild(this._dataErrorIcon);
            }
            this._dataErrorTextField.removeEventListener(TextEvent.LINK,this.onTextLink);
            return;
         }
         this.removeEvent();
         this._isInit = false;
         this._videoDataArray = [];
         if(this._timer.running)
         {
            this._timer.stop();
         }
         if(this._videoClickTimer.running)
         {
            this._videoClickTimer.stop();
         }
         var _loc1_:* = 0;
         while(_loc1_ < this._relativePanelArray.length)
         {
            _loc2_ = this._relativePanelArray[_loc1_];
            Tweener.killTweensOf(_loc2_);
            _loc2_.removeAllBox();
            _loc1_++;
         }
         this._loadImgList.close();
         this._interaction.uninstall();
      }
   }
}
